/**
 * 
 */
package com.risetek.tft.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

import com.risetek.tft.context.Constants;
import com.risetek.tft.context.Sysconfig;
import com.zlinepay.framework.bean.AjaxJson;
import com.zlinepay.framework.bean.DataGrid;
import com.zlinepay.framework.bean.DataGridJson;

/**
 * @author  
 * 
 */
/*@Controller
@RequestMapping("/template")*/
public class TemplateAction extends BaseAction implements ServletContextAware {
	
	private static String ROOT = "/WEB-INF/template/front/"+Sysconfig.get(Constants.TEMPLATEFOLDER);
	
	private ServletContext context;
	
	public void setServletContext(ServletContext servletContext) {
		this.context = servletContext;
	}
	
	@RequestMapping(value = "/edit.action")
	public String edit(String file, ModelMap model) {
		if(StringUtils.isNotEmpty(file)) {
			String templatePath = context.getRealPath(ROOT+file);
			File templateFile = new File(templatePath);
			model.addAttribute("file", file.substring(0, file.lastIndexOf(".")));
			try {
				model.addAttribute("content", FileUtils.readFileToString(templateFile, "UTF-8"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "template/edit";
	}

	@RequestMapping(value = "/list.action")
	@ResponseBody
	public DataGridJson list(DataGrid dataGrid, HttpServletRequest request,
			ModelMap model) {
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		String templatePath = context.getRealPath(ROOT);
		int preLength = templatePath.length();
		File directory = new File(templatePath);
		Iterator<File> it = FileUtils.iterateFiles(directory, null, true);
		while(it.hasNext()) {
			File file = it.next();
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", DigestUtils.md5Hex(file.getPath().substring(preLength)));
			map.put("filename", file.getPath().substring(preLength).replaceAll("\\\\", "/"));
			map.put("isFile", file.isFile() ? "1" : "0");
			list.add(map);
		}
//		if (StringUtils.isNotEmpty(dataGrid.getSort())) {
//			Collections.sort(list, new Comparable<Map<String, String>>() {
//				public int compareTo(Map<String, String> o) {
//					return 0;
//				}
//			});
//		}
		DataGridJson json = new DataGridJson();
		json.setRows(list);
		return json;
	}

	@RequestMapping(value = "/save.action")
	@ResponseBody
	public AjaxJson save(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		AjaxJson json = new AjaxJson();
		String file = request.getParameter("file");
		if(StringUtils.isEmpty(file)) {
			json.setMsg("文件名称不能为空！");
		} else {
			String content = request.getParameter("content");
			String templatePath = context.getRealPath(ROOT+file+".ftl");
			File templateFile = new File(templatePath);
			if(!templateFile.getParentFile().exists()) {
				templateFile.getParentFile().mkdir();
			}
			FileUtils.writeStringToFile(templateFile, content, "UTF-8");
			json.setSuccess(true);
			json.setMsg("保存成功！");
		}
		return json;
	}

	@RequestMapping(value = "/remove.action")
	@ResponseBody
	public AjaxJson del(String[] ids, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		AjaxJson ajaxJson = new AjaxJson();
		try {
			for(String file : ids) {
				String templatePath = context.getRealPath(ROOT+file);
				File templateFile = new File(templatePath);
				templateFile.delete();
			}
			ajaxJson.setSuccess(true);
			ajaxJson.setMsg("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setMsg("删除失败！");
		}
		return ajaxJson;
	}

}
